package com.cgrs572.shortlink.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.cgrs572.shortlink.admin.common.convention.result.Result;
import com.cgrs572.shortlink.admin.common.convention.result.Results;
import com.cgrs572.shortlink.admin.dto.req.UserLoginReqDTO;
import com.cgrs572.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.cgrs572.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.cgrs572.shortlink.admin.dto.resp.UserActualRespDTO;
import com.cgrs572.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.cgrs572.shortlink.admin.dto.resp.UserRespDTO;
import com.cgrs572.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理表现层（控制层）
 */
@RestController
@RequiredArgsConstructor
public class UserController {
    private  final UserService userService;
    /**
     * 根据用户名查询用户信息---返回脱敏数据
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable String username) {
        // 将返回的对象自动处理为JSON数据并返回给前端
        return Results.success(userService.getUserByUserName(username));
        /**
         *  等同于
         *  UserRespDTO result = userService.getUserByUserName(username);
         *  return Results.success(result);
         */
    }

    /**
     * 根据用户名查询用户信息---返回未脱敏数据
     */
    @GetMapping("/api/short-link/admin/v1/actual/user/{username}")
    public Result<UserActualRespDTO> getActualUserByUsername(@PathVariable String username) {
        // 将返回的对象自动处理为JSON数据并返回给前端
        return Results.success(BeanUtil.toBean(userService.getUserByUserName(username), UserActualRespDTO.class));
        /**
         *  等同于
         *  UserRespDTO result = userService.getUserByUserName(username);
         *  UserActualRespDTO re = BeanUtil.toBean(result, UserActualRespDTO.class)
         *  return Results.success(re);
         */
    }

    /**
     * 查询用户名是否存在
     */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username) {
        return Results.success(userService.hasUsername(username));
    }

    /**
     * 注册用户
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam) {
        userService.register(requestParam);
        return Results.success();
    }

    /**
     * 修改用户
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam) {
        userService.update(requestParam);
        return Results.success();
    }

    /**
     * 用户登录
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam) {
        return Results.success(userService.login(requestParam));
    }

    /**
     * 检查用户是否登录
     */
    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username, @RequestParam("token") String token) {
        return Results.success(userService.checkLogin(username, token));
    }

    /**
     * 用户退出登录
     */
    @DeleteMapping("/api/short-link/admin/v1/user/logOut")
    public Result<Void> logOut(@RequestParam("username") String username, @RequestParam("token") String token) {
        userService.logOut(username, token);
        return Results.success();
    }
}
